export const asyncMethods = [
  "canvasGetImageData",
  "canvasPutImageData",
  "canvasToTempFilePath",
  "setEnableDebug",
  "startAccelerometer",
  "stopAccelerometer",
  "getBatteryInfo",
  "getClipboardData",
  "setClipboardData",
  "startCompass",
  "stopCompass",
  "addPhoneContact",
  "startGyroscope",
  "stopGyroscope",
  "startBeaconDiscovery",
  "stopBeaconDiscovery",
  "getBeacons",
  "startLocalServiceDiscovery",
  "stopLocalServiceDiscovery",
  "startDeviceMotionListening",
  "stopDeviceMotionListening",
  "getNetworkType",
  "makePhoneCall",
  "scanCode",
  "getSystemInfo",
  "vibrateShort",
  "vibrateLong",
  "getExtConfig",
  "chooseLocation",
  "getLocation",
  "openLocation",
  "chooseMessageFile",
  "loadFontFace",
  "chooseImage",
  "previewImage",
  "getImageInfo",
  "saveImageToPhotosAlbum",
  "compressImage",
  "chooseVideo",
  "saveVideoToPhotosAlbum",
  "downloadFile",
  "request",
  "connectSocket",
  "closeSocket",
  "sendSocketMessage",
  "uploadFile",
  "login",
  "checkSession",
  "chooseAddress",
  "authorize",
  "addCard",
  "openCard",
  "chooseInvoice",
  "chooseInvoiceTitle",
  "getUserInfo",
  "requestPayment",
  "getWeRunData",
  "showModal",
  "showToast",
  "hideToast",
  "showLoading",
  "hideLoading",
  "showActionSheet",
  "pageScrollTo",
  "startPullDownRefresh",
  "stopPullDownRefresh",
  "setBackgroundColor",
  "setBackgroundTextStyle",
  "setTabBarBadge",
  "removeTabBarBadge",
  "showTabBarRedDot",
  "hideTabBarRedDot",
  "showTabBar",
  "hideTabBar",
  "setTabBarStyle",
  "setTabBarItem",
  "setTopBarText",
  "saveFile",
  "openDocument",
  "getSavedFileList",
  "getSavedFileInfo",
  "removeSavedFile",
  "getFileInfo",
  "getStorage",
  "setStorage",
  "removeStorage",
  "clearStorage",
  "getStorageInfo",
  "closeBLEConnection",
  "closeBluetoothAdapter",
  "createBLEConnection",
  "getBLEDeviceRSSI",
  "getBLEDeviceCharacteristics",
  "getBLEDeviceServices",
  "getBluetoothAdapterState",
  "getBluetoothDevices",
  "getConnectedBluetoothDevices",
  "notifyBLECharacteristicValueChange",
  "openBluetoothAdapter",
  "readBLECharacteristicValue",
  "startBluetoothDevicesDiscovery",
  "stopBluetoothDevicesDiscovery",
  "writeBLECharacteristicValue",
  "getHCEState",
  "sendHCEMessage",
  "startHCE",
  "stopHCE",
  "getScreenBrightness",
  "setKeepScreenOn",
  "setScreenBrightness",
  "connectWifi",
  "getConnectedWifi",
  "getWifiList",
  "setWifiList",
  "startWifi",
  "stopWifi",
  "getBackgroundAudioPlayerState",
  "playBackgroundAudio",
  "pauseBackgroundAudio",
  "seekBackgroundAudio",
  "stopBackgroundAudio",
  "getAvailableAudioSources",
  "startRecord",
  "stopRecord",
  "setInnerAudioOption",
  "playVoice",
  "pauseVoice",
  "stopVoice",
  "getSetting",
  "openSetting",
  "getShareInfo",
  "hideShareMenu",
  "showShareMenu",
  "updateShareMenu",
  "checkIsSoterEnrolledInDevice",
  "checkIsSupportSoterAuthentication",
  "startSoterAuthentication",
  "navigateBackMiniProgram",
  "navigateToMiniProgram",
  "setNavigationBarTitle",
  "showNavigationBarLoading",
  "hideNavigationBarLoading",
  "setNavigationBarColor",
  "redirectTo",
  "reLaunch",
  "navigateTo",
  "switchTab",
  "navigateBack"
];

if (!Promise.prototype.finally) {
  Promise.prototype.finally = function (callback) {
    const P = this.constructor;
    return this.then(
      (value) => P.resolve(callback()).then(() => value),
      (reason) => P.resolve(callback()).then(() => {
        throw reason;
      })
    );
  };
}

export const promisify = (asyncMethod) => {
  return (args = {}) => {
    return new Promise((resolve, reject) => {
      asyncMethod(Object.assign({}, {
        success: resolve,
        fail: reject
      }, args));
    });
  };
};

export const promisifyAll = () => {
  uni.pro = {};

  for (const method of asyncMethods) {
    uni.pro[method] = promisify(uni[method]);
  }
};
